package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.OverduePhaseStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.ibiz.dh.IDhOverdueLetterLawyersService;
import com.zjdiepu.www.ibiz.dh.IDhOverdueListService;
import com.zjdiepu.www.ibiz.third.net.IDeliverService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.model.dh.dto.DhOverdueLetterLawyers;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.ext.DhOverdueLetterLawyersExt;
import com.zjdiepu.www.model.third.request.ali.AliDeliverRequest;
import com.zjdiepu.www.model.third.response.DeliverResponse;
import com.zjdiepu.www.model.third.response.ali.AliDeliverResponseForShow;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;

/**
 * 逾期_律师函Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月21日 14时14分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhOverdueLetterLawyersController", produces="application/json;charset=UTF-8")
@Api(tags = {"逾期_律师函"}, produces="application/json;charset=UTF-8")
public class DhOverdueLetterLawyersController extends BaseController {
    @Resource
    private IDhOverdueLetterLawyersService dhOverdueLetterLawyersService;
    @Resource
    private IDeliverService<AliDeliverRequest, AliDeliverResponseForShow> deliverService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private IDhOverdueListService dhOverdueListService;
    
   /**
    * @author xiongxiaotun <br/>
    * @Title: queryAndSaveExpress  <br/>
    * @Description: 查询并保存快递信息 <br/>
    * @param nu 运单号
    * @return
    * @version V1.0  <br/>
    */
    @ApiOperation(value = "查询并保存律师函快递信息", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "nu", value = "物流运单号", paramType="query", required = true, example="429002364853")
    })
    @RequestMapping(value = "saveExpress", method = RequestMethod.POST)
    public ApiResponse saveExpress(String nu){
    	AbstractSysUser currentUser = getCurrentUser();
		String letterLawyersId = dhOverdueLetterLawyersService.saveExpress(nu, currentUser);
		return ApiResponse.success(letterLawyersId);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增逾期_律师函 <br/>
     * @param dhOverdueLetterLawyers
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增逾期_律师函", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhOverdueLetterLawyers dhOverdueLetterLawyers) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhOverdueLetterLawyers.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhOverdueLetterLawyers.setModifierName(currentUser.getName());
        dhOverdueLetterLawyersService.saveUpdateAndDelete(dhOverdueLetterLawyers, OperateType.add);
        
        //更新逾期信息表逾期阶段
        DhOverdueList dhOverdueList= new DhOverdueList();
        dhOverdueList.setBusiId(dhOverdueLetterLawyers.getBusiId());
        DhOverdueList dhOverdueList1=dhOverdueListService.selectRefined(dhOverdueList);
        dhOverdueList1.setPhaseOverdue(OverduePhaseStatus.SHANG_MEN_CUI_SHOU.getCode());
        dhOverdueListService.saveUpdateAndDelete(dhOverdueList1, OperateType.edit);
        //更新到业务信息-附加信息表电催人id
        YwBusinessExtras ywBusinessExtras= new YwBusinessExtras();
        ywBusinessExtras.setBusiId(dhOverdueLetterLawyers.getBusiId());
        ywBusinessExtras.setTypeExtras("overdue");
        ywBusinessExtras.setCodeExtras("phase_overdue");
        YwBusinessExtras ywBusinessExtras1=ywBusinessExtrasService.selectRefined(ywBusinessExtras);
        if(ywBusinessExtras1!=null){
	        ywBusinessExtras1.setValueExtras(OverduePhaseStatus.SHANG_MEN_CUI_SHOU.getCode());
	    	ywBusinessExtrasService.saveUpdateAndDelete(ywBusinessExtras1, OperateType.edit);
        }
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除逾期_律师函<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除逾期_律师函", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhOverdueLetterLawyersService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用逾期_律师函<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用逾期_律师函", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = false, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	List<String> ids = Arrays.asList(id.split(","));
    	AbstractSysUser currentUser = getCurrentUser();
    	dhOverdueLetterLawyersService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable  <br/>
     * @Description: 启用逾期_律师函<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用逾期_律师函", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhOverdueLetterLawyersService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改逾期_律师函 <br/>
     * @param dhOverdueLetterLawyers
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改逾期_律师函", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhOverdueLetterLawyers dhOverdueLetterLawyers) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhOverdueLetterLawyers.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhOverdueLetterLawyers.setModifierName(currentUser.getName());
    	dhOverdueLetterLawyersService.saveUpdateAndDelete(dhOverdueLetterLawyers, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询逾期_律师函<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询逾期_律师函", response = DhOverdueLetterLawyers.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhOverdueLetterLawyers dhOverdueLetterLawyers = dhOverdueLetterLawyersService.selectById(id);
       if(null == dhOverdueLetterLawyers) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhOverdueLetterLawyers);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询逾期_律师函 <br/>
     * @param dhOverdueLetterLawyersExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询逾期_律师函",response = DhOverdueLetterLawyers.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhOverdueLetterLawyersExt dhOverdueLetterLawyersExt){
        dhOverdueLetterLawyersExt.notDeleted();
        PageVo page = getPage(dhOverdueLetterLawyersExt);
        int records = dhOverdueLetterLawyersService.countByExt(dhOverdueLetterLawyersExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhOverdueLetterLawyers> dpBaseDataDictionaries = dhOverdueLetterLawyersService.selectByExt(dhOverdueLetterLawyersExt);
            for (DhOverdueLetterLawyers dhOverdueLetterLawyers : dpBaseDataDictionaries) {
            	dhOverdueLetterLawyers.setJbTime(ZhppDateUtil.stampToDate(dhOverdueLetterLawyers.getCreated(), "yyyy-MM-dd"));
			}
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "查询快递单状态", response = DeliverResponse.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "queryKuaiDiStutus", method = RequestMethod.POST)
    public ApiResponse queryKuaiDiStutus(String no){
    	if(StringUtils.isBlank(no)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	DeliverResponse res=null;
		try {
			res = deliverService.saveExpInfo(no, "");
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
		} catch (HttpClientException e) {
			e.printStackTrace();
		}
       if(null == res) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(res);
    }
}
